package com.huluobo.module_test_agency.ui.activity

import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.huluobo.appbase.config.ArouterConfig
import com.huluobo.appbase.config.ParametersConfig
import com.huluobo.appbase.config.RequestCodeConfig
import com.huluobo.appbase.config.ResultCodeConfig
import com.huluobo.appbase.ui.BaseActivity
import com.huluobo.appbase.utils.DialogLoadingUtil
import com.huluobo.module_city.ui.activity.CityDataActivity
import com.huluobo.module_test_agency.databinding.ActivityAgencyMessageBinding
import com.huluobo.module_test_agency.ui.adapter.AgencyMessageAdapter
import com.huluobo.module_test_agency.viewmodel.AgencyViewModel

/**
 * Created by lc on 2022/12/29.
 */
@Route(path = ArouterConfig.AGENCY_MESSAGE)
class AgencyMessageActivity : BaseActivity<ActivityAgencyMessageBinding>() {
    override fun getViewBinding(): ActivityAgencyMessageBinding =
        ActivityAgencyMessageBinding.inflate(layoutInflater)

    private val agencyViewModel by viewModels<AgencyViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    private fun initView() {
        mViewBinding.toolBar.setOnClickListener {
            finish()
        }

        mViewBinding.tvCity.setOnClickListener {
//            ARouter.getInstance().build(ArouterConfig.CITY_DATA)
//                .navigation(this, RequestCodeConfig.REQUEST_CODE_SELECT_CITY)
            val intent = Intent(this, CityDataActivity::class.java)
            startActivityForResult(intent, RequestCodeConfig.REQUEST_CODE_SELECT_CITY)

        }
        val layoutManager = LinearLayoutManager(this)
        mViewBinding.rvData.layoutManager = layoutManager
    }

    private fun loadData(cityId: String) {
        DialogLoadingUtil.showLoading(this, "请稍后")
        agencyViewModel.loadTestAgencyMessage(cityId).observe(this) {
            it?.let {
                if (it.error_code == 0) {
                    //请求成功
                    it.result?.data?.let { data ->
                        val agencyMessageAdapter =
                            AgencyMessageAdapter(data, callback = { position, data ->
                                //以后可以在这里做拓展,比如打电话,导航等
                            })
                        mViewBinding.rvData.adapter = agencyMessageAdapter
                        DialogLoadingUtil.cancel()
                    }
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            RequestCodeConfig.REQUEST_CODE_SELECT_CITY -> {
                when (resultCode) {
                    ResultCodeConfig.RESULT_CODE_SELECT_CITY_SUCCESS -> {
                        val cityId = data?.getStringExtra(ParametersConfig.CITY_ID)
                        val cityName = data?.getStringExtra(ParametersConfig.CITY_NAME)
                        mViewBinding.tvCity.text = cityName
                        cityId?.let {
                            loadData(it)
                        }
                    }
                }
            }
        }
    }
}